﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using CoreData;
using EntityData;
using Helper;
using Services.BusinessRules;
namespace Services
{

    public partial class Product_AttributeService
    {
        readonly IProduct_AttributeEntity entity;

        public Product_AttributeService()
        {
            this.entity = new Product_AttributeEntity();

        }

        #region Main Method

        /// <summary>
        /// Save 
        /// </summary>
        /// <param name="_model"></param>
        /// <returns></returns>
        public long Save(object _model, ref string mess)
        {
            try
            {
                if (Validate((Product_Attribute)_model, ref mess))
                {
                    var id = long.Parse(_model.GetType().GetProperty("ID").GetValue(_model, null).ToString());
                    if (id == 0)
                    {
                        return entity.Save((CoreData.Product_Attribute)_model, Table.Product_Attribute.ToString());
                    }
                    else
                    {
                        return entity.Update((CoreData.Product_Attribute)_model, Table.Product_Attribute.ToString());
                    }
                }
                else
                {
                    return -1;
                }
            }
            catch { return -1; }
        }

        /// <summary>
        /// Get by ID
        /// </summary>
        /// <param name="_id"></param>
        /// <returns></returns>
        public CoreData.Product_Attribute GetByID(long _id)
        {
            try
            {
                return entity.GetById(_id, Table.Product_Attribute.ToString());
            }
            catch { return null; }
        }

        /// <summary>
        /// Get List
        /// </summary>
        /// <returns></returns>
        public IList<CoreData.Product_Attribute> GetList()
        {
            try
            {
                return entity.GetAll(Table.Product_Attribute.ToString()).ToList();
            }
            catch { return null; }

        }

        /// <summary>
        /// Get List with Linq
        /// </summary>
        /// <returns></returns>
        public IList<CoreData.Product_Attribute> GetListByLINQ(Func<CoreData.Product_Attribute, Boolean> _where)
        {
            try
            {
                return entity.GetMany(_where, Table.Product_Attribute.ToString()).ToList();
            }
            catch { return null; }
        }
        /// <summary>
        /// Get One with Linq
        /// </summary>
        /// <returns></returns>
        public CoreData.Product_Attribute GetOneByLINQ(Func<CoreData.Product_Attribute, Boolean> _where)
        {
            try
            {
                return entity.Get(_where, Table.Product_Attribute.ToString());
            }
            catch { return null; }
        }

        /// <summary>
        /// Get List with search string
        /// </summary>
        /// <returns></returns>
        public IList<CoreData.Product_Attribute> GetList(string _searchstring)
        {
            //search theo tieu chi nao do         
            try
            {
                return entity.GetBySearchString(_searchstring, Table.Product_Attribute.ToString()).ToList();
            }
            catch { return null; }
        }


        /// <summary>
        /// Delete
        /// </summary>
        /// <param name="_model"></param>
        /// <returns></returns>
        public bool Delete(object _model)
        {
            try
            {
                entity.Delete((CoreData.Product_Attribute)_model, Table.Product_Attribute.ToString());
                return true;
            }
            catch { return false; }
        }


        #endregion



        #region Other Method

        bool Validate(Product_Attribute obj, ref string mess)
        {
            
            new ValidateRequired("Giá trị", "Name", obj.Value);
            if (obj.ProductID == 0)
            {
                new ValidateRequired("Chưa chọn hàng hóa", "Đơn vị nhập", "");
            }
            if (obj.AttributeID == 0)
            {
                new ValidateRequired("Chưa chọn thuộc tính", "Đơn vị nhập", "");
            }
            new ValidateExist("Mã hàng hóa", "Code", "Code", obj, GetList().Cast<object>().ToList());
            //   new ValidateExist("Tên học viên", "Name", "Name", obj, GetList().Cast<object>().ToList());
            new ValidateLenght("Giá trị", "Value", obj.Value, 255);
            mess = BusinessObject.Validate();
            BusinessObject.Clear();
            return mess == null ? true : false;
        }

        #endregion

    }

}
